package com.ezops.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.mvel2.optimizers.impl.refl.nodes.ArrayLength;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.ezops.dao.facade.IReportDataTransferFacade;
import com.ezops.dao.facade.ISystemPropertyFacade;
import com.ezops.dto.RemoteRequest;
import com.ezops.filter.ReportCriteria;
import com.ezops.filter.ReportCriteria.Filter;
import com.ezops.filter.ReportCriteriaImpl;
import com.ezops.gateway.DefaultReportCallBackHandler;
import com.ezops.gateway.EzopsRemoteGateway;
import com.ezops.models.SystemProperty;
import com.ezops.service.IReportSchedulerService;
import com.ezops.util.EzopsReportsProvider;

/**
 * This class uses spring scheduling capabilities to schedule the report request
 * generation.
 * <p>
 * TODO : This is using the default thread pool configuration provided by spring
 * which is one thread per pool. So in future defined the custom thread pool
 * properties.
 * 
 * @author vgaurav
 * 
 */
@Service
@EnableScheduling
public class ReportSchedulerService implements IReportSchedulerService {

	@Autowired
	private IReportDataTransferFacade reportFacade;
	@Autowired
	private EzopsRemoteGateway gateway;
	@Autowired
	private ISystemPropertyFacade systemPropertyFacade;
	DateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
	SystemProperty property = new SystemProperty();
	Calendar cal = Calendar.getInstance();

	@Scheduled(cron = "${lifecyclereport.interval}")
	@Override
	public void sendLifeCycleReportRequest() {
		generateEODReports();

	}

	/**
	 * Send request to remote server for generating EOD reports.
	 */
	public void generateEODReports() {
		// List<Report> allReports = reportFacade.findAll();
		// for(Report report : allReports){

		// }
		ReportCriteria criteria = ReportCriteriaImpl.createInstance()
				.addFilter(Filter.REPORT_NAME, EzopsReportsProvider.MASTER_AGGREMENT.getFileName())
				.addFilter(Filter.REPORT_DISPLAY_NAME, EzopsReportsProvider.MASTER_AGGREMENT.getDisplayName());
		RemoteRequest req = criteria.build();
		gateway.sendRemoteRequest(req, new DefaultReportCallBackHandler(
				criteria));
		
		property.setName("asofdate");
	    //cal.add(Calendar.DATE, -1);
		property.setValue(format1.format(new Date().getTime() - 24*3600*1000));
		systemPropertyFacade.updateEodReportTime(property);

	}

}
